#ifndef __BUTTON_H_
#define __BUTTON_H_

#include <android.h>
#include "calc.h"
#include "display.h"
#define TEXT_MAXSIZE 16

//按钮结构体
typedef struct 
{
int  id;                 //按钮id
int  type;               //按钮类型，显示：1，隐藏：0
int  isDraw;             //是否绘制
int  isClick;            //是否按下
int  x;                  //按钮位置x
int  y;                  //按钮位置y
int  w;                  //按钮宽度w
int  h;                  //按钮高度h
int  R;                  //按钮边缘颜色R
int  G;                  //按钮边缘颜色G
int  B;                  //按钮边缘颜色B
int  r;                  //按钮中间颜色r
int  g;                  //按钮中间颜色g
int  b;                  //按钮中间颜色b
int  _R;                 //按钮按下时边缘颜色R
int  _G;                 //按钮按下时边缘颜色G
int  _B;                 //按钮按下时边缘颜色B
int  _r;                 //按钮按下时中间颜色r
int  _g;                 //按钮按下时中间颜色g
int  _b;                 //按钮按下时中间颜色b
char text[TEXT_MAXSIZE]; //按钮文字，注意文字数量，15个字符，或者7个汉字
char value;
int  textX;              //文字x坐标
int  textY;              //文字y坐标
int  textW;              //文字宽度
int  textH;              //文字高度
int  textR;              //文字颜色R
int  textG;              //文字颜色G
int  textB;              //文字颜色B
int  _textR;             //按键按下时文字颜色R
int  _textG;             //按键按下时文字颜色G
int  _textB;             //按键按下时文字颜色B
int  textLen;            //文字长度
int  textFont;           //文字大小
int  bitmap;             //图形句柄
int  bitmapX;            //图形x坐标
int  bitmapY;            //图形y坐标
int  bitmapW;            //图形宽度
int  bitmapH;            //图形高度
}button;

int buttonId = 0;

//新增带文字的按钮
void buttonCreate(button *btn,char *text ,char value, int x,int y,int w,int h,int RGB,int rgb,int _RGB,int _rgb,int textRGB,int _textRGB,int textFont)
{
    if(strlen(text)<TEXT_MAXSIZE)
    {
        strcpy(btn->text,text);
    }
    else
    {
        strncpy(btn->text,text,TEXT_MAXSIZE-1);
    }
    btn->textLen = strlen(btn->text);
    textwh(btn->text, 0, btn->textFont, &btn->textW, &btn->textH);
    btn->text[btn->textLen]=0;
    btn->value = value;
    btn->x=x; 
    btn->y=y;
    btn->w=w;
    btn->h=h;
    btn->R=RGB/65536%256;
    btn->G=RGB/256%256;
    btn->B=RGB%256;
    btn->r=rgb/65536%256;
    btn->g=rgb/256%256;
    btn->b=rgb%256;
    btn->_R=_RGB/65536%256;
    btn->_G=_RGB/256%256;
    btn->_B=_RGB%256;
    btn->_r=_rgb/65536%256;
    btn->_g=_rgb/256%256;
    btn->_b=_rgb%256;
    btn->textR=textRGB/65536%256;
    btn->textG=textRGB/256%256;
    btn->textB=textRGB%256;
    btn->_textR=_textRGB/65536%256;
    btn->_textG=_textRGB/256%256;
    btn->_textB=_textRGB%256;
    btn->type=1;
    btn->id=buttonId;
    buttonId++;
    btn->textX=btn->x+(btn->w - btn->textW)/2;
    btn->textY=btn->y+(btn->h - btn->textH)/2;
    btn->bitmap=-1;//不显示图片
}

int buttonSetValue(button *btn, char value)
{
    if(btn != NULL)
    {
        btn->value = value;
        return 1;
    }
    return 0;
}

//显示按钮 参数：按钮结构体指针
void buttonDraw(button *btn)
{
    if(btn->type == 1)
    {
        if(btn->isClick == 0)
        {
            //绘制一个矩形
            drect(btn->x,btn->y,btn->w,btn->h,btn->R,btn->G,btn->B);
            drect(btn->x+5,btn->y+5,btn->w-10,btn->h-10,btn->r,btn->g,btn->b);
            //绘制文字或图形
            if(btn->textLen!=0)
            {
                dtext(btn->text,btn->textX,btn->textY,btn->textR,btn->textG,btn->textB,0,btn->textFont);
            }
        }
        
        if(btn->isClick == 1)
        {
            //绘制一个矩形
            drect(btn->x,btn->y,btn->w,btn->h,btn->_R,btn->_G,btn->_B);
            drect(btn->x+8,btn->y+8,btn->w-16,btn->h-16,btn->_r,btn->_g,btn->_b);
            //绘制文字或图形
            if(btn->textLen!=0)
            {
                dtext(btn->text,btn->textX,btn->textY,btn->_textR,btn->_textG,btn->_textB,0,btn->textFont);
            }
        }
        
        btn->isDraw = 1;
    }
}

//显示一个按钮
void buttonShow(button *btn)
{
    if(btn->type == 1 && btn->isDraw == 1)
    {
        ref(btn->x, btn->y, btn->w, btn->h);
    }
}

//显示所有的按键
int buttonShowAll(button *btn, int buttonNum)
{
    if(btn != NULL)
    {
        int i;
        for(i = 0;i<buttonNum;i++)
        {
            buttonDraw(&btn[i]);
            buttonShow(&btn[i]);
        }
        return 1;
    }
    return 0;
}

//判断按钮点击事件 参数：按钮指针 触屏x坐标 触屏y坐标
//返回值：按键id值，返回-1说明按键未被点击
int buttonIsClick(button *btn,int x,int y)
{
    if(btn != NULL && btn->type == 1)
    {
        if(x>btn->x && x<btn->x+btn->w && y>btn->y && y<btn->y+btn->h)
        {
            btn->isClick = 1;
            return btn->id;
        }
        else
        {
            btn->isClick = 0;
            return -1;
        }
    }
    return -1;
}

//松开按键
int buttonIsRelease(button *btn)
{
    if(btn != NULL && btn->type == 1 && btn->isClick == 1)
    {
        btn->isClick = 0;
        return btn->id;
    }
    return -1;
}

//设置按钮文字 参数：按钮结构体指针 新的按钮文字
int buttonSetText(button *btn,char *text)
{
    if(btn != NULL)
    {
        if(strlen(text)<TEXT_MAXSIZE)
        {
            strcpy(btn->text,text);
        }
        else
        {
            strncpy(btn->text,text,TEXT_MAXSIZE-1);
        }
        btn->textLen = strlen(btn->text);
        textwh(btn->text, 0, btn->textFont, &btn->textW, &btn->textH);
        btn->text[textLen]=0;
        btn->textX=btn->x+(btn->w - btn->textW)/2;
        btn->textY=btn->y+(btn->h - btn->textH)/2;
        return 1;
    }
    return 0;
}

int charIsprint(char value)
{
    int i;
    char *printString = "0123456789+-*/()";
    for(i=0;i<strlen(printString);i++)
    {
        if(value == printString[i])
        {
            return 1;
        }
    }
    return 0;
}

void buttonEvent(button *btn, int buttonNum, int *buttonClickId,char *exp, int expLen, int type, int p1, int p2)
{
    int i,j,len,result,expIsTrue;
    char res[12];
    queue expression,rpnList;
    queueInit(&expression);
    queueInit(&rpnList);
    if(type == MS_DOWN)
    {
        for(i = 0;i < buttonNum;i++)
        {
            *buttonClickId = buttonIsClick(&btn[i], p1, p2);
            if(*buttonClickId!=-1)
            {
                buttonDraw(&btn[i]);
                buttonShow(&btn[i]);
                if(charIsprint(btn[i].value))
                {
                    for(j=0;j<expLen;j++)
                    {
                        if(exp[j] == 0)
                        {
                            exp[j] = btn[i].value;
                            j=expLen;
                        }
                    }
                    textDisplay(exp);
                }
                
                if(btn[i].value == 'C')
                {
                    for(j=0;j<expLen;j++)
                    {
                        if(exp[j] != 0)
                        {
                            exp[j] = 0;
                        }
                    }
                    textDisplay(exp);
                }
                else if(btn[i].value == 'D')
                {
                    for(j=0;j<expLen;j++)
                    {
                        if(exp[j] == 0)
                        {
                            exp[j-1] = 0;
                            j=expLen;
                        }
                    }
                    textDisplay(exp);
                }
                else if(btn[i].value == '=')
                {
                    for(j=0;j<expLen;j++)
                    {
                        if(exp[j] == 0)
                        {
                            len = j;
                            j=expLen;
                        }
                    }
                    stringToQueue(exp,len,&expression);
                    for(j=0;j<expLen;j++)
                    {
                        if(exp[j] != 0)
                        {
                            exp[j] = 0;
                        }
                    }
                    if(getRpnList(&expression,&rpnList)==1)
                    {
                        result = solveRpnList(&rpnList,&expIsTrue);
                        if(expIsTrue)
                        {
                            sprintf(res,"%d",result);
                            textDisplay(res);
                        }
                    }
                    else
                    {
                        textDisplay("ERROR");
                    }
                    //textDisplay(exp);
                }
                else if(btn[i].value == 0)
                {
                    toast("暂不支持此按键",0);
                }
                break;
            }
        }
    }

    if(type == MS_UP)
    {
        for(i = 0;i < buttonNum;i++)
        {
            *buttonClickId = buttonIsRelease(&btn[i]);
            if(*buttonClickId != -1)
            {
                buttonDraw(&btn[i]);
                buttonShow(&btn[i]);
                //printf("按键松开，releaseButtonId = %d\n",*buttonReleaseId);
                break;
            }
        }
    }
}

#endif



